	list      p=16F88           ; list directive to define processor
	#include <p16f88.inc>        ; processor specific variable definitions
;	errorlevel  -302              ; suppress message 302 from list file
    radix decimal


	__CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_ON & _INTRC_IO
	__CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF

var_main    UDATA_SHR
WREGsave    res 1
STATUSsave  res 1
FSRsave     res 1
PCLATHsave  res 1
char        res 1
char_n      res 1
char_a      res 1
row_n       res 1
rx_n        res 1
rx_d        res 1
port_fix    res 1
tmp         res 1

var_main2 UDATA
string      res 8

font_space equ d'125'
text_end  equ d'52'

STARTUP	CODE
  goto  startup
  nop
  nop
  nop
  goto  ISR

sec_main  CODE

;====================
ISR
;====================
  banksel INTCON
  bcf   INTCON,7

  movwf	WREGsave
  movf	STATUS,W
  movwf	STATUSsave
  movf	PCLATH,W
  movwf	PCLATHsave
  movf	FSR,W
  movwf	FSRsave
;--------------------
; put ISR here

ISR_TMR0
  banksel INTCON
  btfss   INTCON,TMR0IF
  goto    ISR_RX
  bcf     INTCON,TMR0IF
  call    tmr

ISR_RX
  banksel PIR1
  btfss   PIR1,RCIF
  goto    ISR_END
  bcf     PIR1,RCIF
  call    rx

;--------------------
ISR_END
  movf	FSRsave,W
  movwf	FSR
  movf	PCLATHsave,W
  movwf	PCLATH
  movf	STATUSsave,W
  movwf	STATUS
  swapf	WREGsave,F
  swapf	WREGsave,W
  
  banksel INTCON
  bsf   INTCON,7
  retfie
;--------------------

;====================
startup
;====================
;bank 0
  banksel PORTA
  clrf  PORTB
  clrf  PORTA
  clrf  PIR1
    
;  banksel INTCON
  movlw 0<<GIE|1<<PEIE|1<<TMR0IE|0<<RBIE
  movwf INTCON
  
;  banksel RCSTA
  movlw 1<<SPEN|0<<RX9|1<<CREN|0<<ADDEN
  movwf RCSTA
	
;bank 1
  banksel PCON
  bsf   PCON,NOT_POR
  bsf   PCON,NOT_BOR

;  banksel OPTION_REG
  movlw b'11001010'   ;prescale tmr0, prescaler=255
;  movlw b'11000111'   ;prescale tmr0, prescaler=255
  movwf OPTION_REG

  clrf  ANSEL         ;disable adc

;  bsf   WDTCON,SWDTEN
;  bsf   PIE1,RCIE

  movlw b'00110000'   ;500kHz
;  movlw b'00000000'   ;32kHz
  movwf OSCCON

  movlw b'00000000'
  movwf TRISA
  movlw b'00001100'
  movwf TRISB
  
  bsf   TXSTA,BRGH
  movlw d'24'   ;1200baud @ 500kHz
  movwf SPBRG

  bsf   PIE1,RCIE ;enable RX interrupt

  clrf  char_n
  clrf  rx_n
  clrf  tmp
  
  call  init_string
  clrf  char_n
  call  new_char

;  bsf PORTB,4
  banksel INTCON
  bsf INTCON,GIE
  goto main
;--------------------

init_string
  banksel string
  movf  char_n,w
  addlw string
  movwf FSR
  
  movf  char_n,w
;  pagesel text
  call  text
  movwf INDF
  incf  char_n,w
  andlw b'00000111'
  movwf char_n
  btfss STATUS,Z
  goto  is_2
  movlw text_end
  movwf INDF
is_2
  movf  INDF,w
  xorlw text_end
  btfsc STATUS,Z
  return
  goto  init_string
  
;====================
main
;====================
  banksel PORTA
  clrwdt

  movlw b'00000010'
  incf  tmp,f
  btfsc STATUS,Z
  xorwf PORTB,f

  movf  char,w
  
  xorlw text_end
  btfss STATUS,Z
  goto  main
  
;  movlw h'ff'
;  movwf PORTA
  
  banksel INTCON
  bcf INTCON,TMR0IE
    
  banksel TMR0
  clrf  TMR0
  
  clrf  char_n
  call  new_char
  
p_loop
  clrwdt
;  banksel INTCON
;  btfss INTCON,TMR0IF
;  goto  p_loop2
;  goto  p_loop_end
p_loop2
  banksel PORTB
  btfss PORTB,3   ;INDEX PULSE
  goto  p_loop

p_loop_end
  
  banksel INTCON
  bcf   INTCON,TMR0IF
  clrf  TMR0
  bsf   INTCON,TMR0IE
  
  goto main
;--------------------

new_char
;  banksel PORTB
;  movlw b'00000010'
;  xorwf PORTB,f
  
  banksel string
  movf  char_n,w
  addlw string
  movwf FSR
  movf  INDF,w
  movwf char
  
;  pagesel tab5
  call  tab5  ;get address of char in font
  
  movwf char_a
  incf  char_n,w
  andlw b'00000111'
  movwf char_n
  clrf  row_n
  return

;====================
rx
;====================
  banksel RCREG
  
  movlw b'00000001'
  xorwf PORTB,f
  
  movf  RCREG,w
  movwf rx_d
  btfss rx_d,7
  goto  add_char
  clrf  rx_n
  return
  
add_char
  banksel string
  movf  rx_n,w
  addlw string
  movwf FSR
  
  
  movf  rx_d,w
  addlw d'250'
  movwf rx_d
  btfsc rx_d,6
  addlw d'165'
  movwf INDF
  
  incf  rx_n,w
  andlw b'00000111'
  movwf rx_n
  addlw string
  movwf FSR
  
  movlw text_end
  movwf INDF
  movwf char
  clrf  char_n
  banksel PORTA
  movf  rx_d,w
  movwf PORTA
  return

;====================
tmr
;====================
  banksel PORTB
  
;  movf  row_n,w
;  movwf PORTA
  
  movf  char_a,w
  addwf row_n,w
;  pagesel font
  call  font
  
  movwf port_fix
  bcf   port_fix,7
  btfsc port_fix,6
  bsf   port_fix,7
  bcf   port_fix,6
  btfsc port_fix,5
  bsf   port_fix,6
  movf  port_fix,w
  movwf PORTA
  
  incf  row_n,f
  movf  row_n,w
  xorlw 5
  btfsc STATUS,Z
  call  short_space
;  movf  row_n,w
;  xorlw 6
;  btfsc STATUS,Z
;  call  short_space
  movf  row_n,w
  xorlw 7
  btfsc STATUS,Z
  call  new_char
  
  bsf   TMR0,7

  return
;--------------------

short_space
  movlw font_space
  movwf char_a
  return

font_data CODE  0xfb

font
  movwf tmp
  movlw HIGH($+4)
  movwf PCLATH
  movf  tmp,w
  addwf PCL,f
; A 0
  retlw b'0000001'
  retlw b'1101110'
  retlw b'1101110'
  retlw b'1101110'
  retlw b'0000001'
; B 1
  retlw b'0000000'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'1001001'
; C 2
  retlw b'1000001'
  retlw b'0111110'
  retlw b'0111110'
  retlw b'0111110'
  retlw b'1011101'
; D 3
  retlw b'0000000'
  retlw b'0111110'
  retlw b'0111110'
  retlw b'1011101'
  retlw b'1100011'
; E 4
  retlw b'0000000'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'0111110'
; F 5
  retlw b'0000000'
  retlw b'1110110'
  retlw b'1110110'
  retlw b'1110110'
  retlw b'1111110'
; G 6
  retlw b'1000001'
  retlw b'0111110'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'0000101'
; H 7
  retlw b'0000000'
  retlw b'1110111'
  retlw b'1110111'
  retlw b'1110111'
  retlw b'0000000'
; I 8
  retlw b'1111111'
  retlw b'0111110'
  retlw b'0000000'
  retlw b'0111110'
  retlw b'1111111'
; J 9
  retlw b'1011111'
  retlw b'0111111'
  retlw b'0111110'
  retlw b'1000000'
  retlw b'1111110'
; K 10
  retlw b'0000000'
  retlw b'1110111'
  retlw b'1101011'
  retlw b'1011101'
  retlw b'0111110'
; L 11
  retlw b'0000000'
  retlw b'0111111'
  retlw b'0111111'
  retlw b'0111111'
  retlw b'0111111'
; M 12
  retlw b'0000000'
  retlw b'1111101'
  retlw b'1110011'
  retlw b'1111101'
  retlw b'0000000'
; N 13
  retlw b'0000000'
  retlw b'1111011'
  retlw b'1110111'
  retlw b'1101111'
  retlw b'0000000'
; O 14
  retlw b'1000001'
  retlw b'0111110'
  retlw b'0111110'
  retlw b'0111110'
  retlw b'1000001'
; P 15
  retlw b'0000000'
  retlw b'1110110'
  retlw b'1110110'
  retlw b'1110110'
  retlw b'1111001'
; Q 16
  retlw b'1000001'
  retlw b'0111110'
  retlw b'0101110'
  retlw b'1011110'
  retlw b'0100001'
; R 17
  retlw b'0000000'
  retlw b'1110110'
  retlw b'1100110'
  retlw b'1010110'
  retlw b'0111001'
; S 18
  retlw b'0111001'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'1001110'
; T 19
  retlw b'1111110'
  retlw b'1111110'
  retlw b'0000000'
  retlw b'1111110'
  retlw b'1111110'
; U 20
  retlw b'1000000'
  retlw b'0111111'
  retlw b'0111111'
  retlw b'0111111'
  retlw b'1000000'
; V 21
  retlw b'1100000'
  retlw b'1011111'
  retlw b'0111111'
  retlw b'1011111'
  retlw b'1100000'
; W 22
  retlw b'1000000'
  retlw b'0111111'
  retlw b'1000111'
  retlw b'0111111'
  retlw b'1000000'
; X 23
  retlw b'0011100'
  retlw b'1101011'
  retlw b'1110111'
  retlw b'1101011'
  retlw b'0011100'
; Y 24
  retlw b'1111000'
  retlw b'1110111'
  retlw b'0001111'
  retlw b'1110111'
  retlw b'1111000'
; Z 25
  retlw b'0011110'
  retlw b'0101110'
  retlw b'0110110'
  retlw b'0111010'
  retlw b'0111100'
  
  ; (space) 26
  retlw b'1111111'
  retlw b'1111111'
  retlw b'1111111'
  retlw b'1111111'
  retlw b'1111111'
; ! 27  
  retlw b'1111111'
  retlw b'0110000'
  retlw b'0110000'
  retlw b'1111111'
  retlw b'1111111'
  
  retlw b'1111111'
  retlw b'1111000'
  retlw b'1111111'
  retlw b'1111000'
  retlw b'1111111'
  
  retlw b'0000001'
  retlw b'1110110'
  retlw b'0000000'
  retlw b'0110110'
  retlw b'0110110'
  
  retlw b'0100011'
  retlw b'1001101'
  retlw b'1010101'
  retlw b'1011001'
  retlw b'1100010'
  
  retlw b'1011100'
  retlw b'1101100'
  retlw b'1110111'
  retlw b'0011011'
  retlw b'0011101'
  
  retlw b'0001111'
  retlw b'1010111'
  retlw b'1011010'
  retlw b'1010111'
  retlw b'0001111'
  
;  retlw b'1111100'
;  retlw b'1001100'
;  retlw b'0110111'
;  retlw b'0110111'
;  retlw b'0110111'
  
  retlw b'1111111'
  retlw b'1001001'
  retlw b'1001001'
  retlw b'1111111'
  retlw b'1111111'
  
  retlw b'1111101'
  retlw b'1111110'
  retlw b'0101110'
  retlw b'1110110'
  retlw b'1111001'
  
  retlw b'1101011'
  retlw b'1110111'
  retlw b'1000001'
  retlw b'1110111'
  retlw b'1101011'
  
  retlw b'1110111'
  retlw b'1110111'
  retlw b'1000001'
  retlw b'1110111'
  retlw b'1110111'
  
  retlw b'1111111'
  retlw b'0101111'
  retlw b'1001111'
  retlw b'1111111'
  retlw b'1111111'
  
  retlw b'1110111'
  retlw b'1110111'
  retlw b'1110111'
  retlw b'1110111'
  retlw b'1110111'
  
  retlw b'1111111'
  retlw b'0011111'
  retlw b'0011111'
  retlw b'1111111'
  retlw b'1111111'
  
  retlw b'1011111'
  retlw b'1101111'
  retlw b'1110111'
  retlw b'1111011'
  retlw b'1111101'
  
; 0
  retlw b'1000001'
  retlw b'0101110'
  retlw b'0110110'
  retlw b'0111010'
  retlw b'1000001'

  retlw b'1111111'
  retlw b'0111101'
  retlw b'0000000'
  retlw b'0111111'
  retlw b'1111111'
  
  retlw b'0111101'
  retlw b'0011110'
  retlw b'0101110'
  retlw b'0110110'
  retlw b'0111001'
  
  retlw b'1011110'
  retlw b'0111110'
  retlw b'0111010'
  retlw b'0110100'
  retlw b'1001110'
  
  retlw b'1100111'
  retlw b'1101011'
  retlw b'1101101'
  retlw b'0000000'
  retlw b'1101111'
  
  retlw b'1011000'
  retlw b'0111010'
  retlw b'0111010'
  retlw b'0111010'
  retlw b'1000110'
  
  retlw b'1000011'
  retlw b'0110101'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'1001111'
  
  retlw b'1111110'
  retlw b'0001110'
  retlw b'1110110'
  retlw b'1111010'
  retlw b'1111100'
  
  retlw b'1001001'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'1001001'
  
  retlw b'1111001'
  retlw b'0110110'
  retlw b'0110110'
  retlw b'1010110'
  retlw b'1100001'
  
  IF ((HIGH ($)) != (HIGH (font+5)))
    ERROR "font CROSSES PAGE BOUNDARY!"
  ENDIF

;text_data CODE 0x05

text
  movwf tmp
  movlw HIGH($)
  movwf PCLATH
  movf  tmp,w
  addwf PCL,f
  retlw 7
  retlw 4
  retlw 11
  retlw 11
  retlw 14
  retlw 27
  retlw text_end
  IF ((HIGH ($)) != (HIGH (text+5)))
    ERROR "text CROSSES PAGE BOUNDARY!"
  ENDIF

tab5
  movwf tmp
  movlw HIGH($)
  movwf PCLATH
  movf  tmp,w
  addwf PCL,f
  retlw 0
  retlw 5
  retlw 10
  retlw 15
  retlw 20
  retlw 25
  retlw 30
  retlw 35
  retlw 40
  retlw 45
  retlw 50
  retlw 55
  retlw 60
  retlw 65
  retlw 70
  retlw 75
  retlw 80
  retlw 85
  retlw 90
  retlw 95
  retlw 100
  retlw 105
  retlw 110
  retlw 115
  retlw 120
  retlw 125
  retlw 130
  retlw 135
  retlw 140
  retlw 145
  retlw 150
  retlw 155
  retlw 160
  retlw 130 ;hack to keep font within 0xff addresses
;  retlw 130
  retlw 165
  retlw 170
  retlw 175
  retlw 180
  retlw 185
  retlw 190
  retlw 195
  retlw 200
  retlw 205
  retlw 210
  retlw 215
  retlw 220
  retlw 225
  retlw 230
  retlw 235
  retlw 240
  retlw 245
  retlw 250
  retlw 130 ;dummy value for string terminator, never used
  
  IF ((HIGH ($)) != (HIGH (tab5+5)))
    ERROR "tab5 CROSSES PAGE BOUNDARY!"
  ENDIF

end
